/**
* 供应商档案-修定
*/
<template>
    <vp-dialog :span="4" ref="editDialogRef">
        <div class="p10">
            <el-tabs type="border-card">
                <el-tab-pane>
                    <template #label><vp-label zh="基本信息" en="Basic Info" /></template>
                    <QualitySupplier ref="qualitySupplierRef" :loading="loading" :type="type">
                    </QualitySupplier>
                    <ProcessHandle ref="processHandleRef" :show-operate="false"></ProcessHandle>
                </el-tab-pane>
            </el-tabs>
            <StatusBar :id="formData.id" :status="formData.zt" :isValid="formData.yxbs" />
            <PageBottom>
                <vp-btn-save :loading="saveLoading" @click="handleSave(10)" />
                <VpProcessToolBar :loading="saveLoading"
                    :set-loading-fn="(_loading: boolean) => { saveLoading = _loading }" :before-show-fn="beforeShowFn"
                    :after-agree-fn="afterAgreeFn" />
            </PageBottom>
        </div>
    </vp-dialog>
</template>
<script setup lang="ts" name="RevisionQualitySupplier">
import { ref, nextTick } from 'vue';
import request from '/@/utils/request';
import QualitySupplier from '../details/QualitySUpplier.vue';
import PageBottom from '/@/components/vp/PageBottom/index.vue';
import { ElMessage } from "element-plus";
import { useRoute } from 'vue-router';
import mittBus from '/@/utils/mitt';
import StatusBar from '/@/components/vp/status/StatusBar.vue';
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';

const loading = ref(false);
const saveLoading = ref(false);
const type = ref('add');

const emits = defineEmits<{
    (e: 'formSubmited'): void
}>()

const qualitySupplierRef = ref();
const processHandleRef = ref();

const route = useRoute();

const formData = ref({
    id: '',
    zt: 10,
    yxbs: 1,
});


const editDialogRef = ref();
const handleOpen = (param: { type: string; id: any; }) => {
    type.value = param.type;
    editDialogRef.value.handleOpen();

    nextTick(() => {
        if (param.type === 'edit') {
            loading.value = true;
            request.get(`/quality/supplier/${param.id}`).then((res) => {
                const data = { ...res.data };
                data.fBbid=data.id;
                qualitySupplierRef.value.setData(data);
                loading.value = false;

                 emits("formSubmited");
            })
        } else {
            qualitySupplierRef.value.setData({
                ...formData,
                ...param,
            });
        }
    });
};

// 保存
const handleSave = (zt: number) => {
    saveLoading.value = true;
    qualitySupplierRef.value.getData(zt).then((data: any) => {
        request.post("/quality/supplier/revision", {
            ...data,
            zt: zt,
            id:null,
            zid:formData.value.id,
        }).then(() => {
            ElMessage.success("操作成功/Operation Succeeded");
            editDialogRef.value.handleClose();

            emits("formSubmited");
        }).finally(() => {
            saveLoading.value = false;
        })
        console.log("data", data);
    }).catch(() => { saveLoading.value = false; });
}


const beforeShowFn = () => {
    saveLoading.value = true;
    return qualitySupplierRef.value.getData(20).finally(() => { saveLoading.value = false; });
}

const afterAgreeFn = (processHandle: any) => {
    saveLoading.value = true;
    qualitySupplierRef.value.getData(20).then((data: any) => {
        request.post("/quality/supplier/revision", {
            ...data,
            zt: 20,
            id:null,
            zid:data.zid,
            processHandle: processHandle,
        }).then(() => {
            ElMessage.success("操作成功/Operation Succeeded");
            editDialogRef.value.handleClose();
        }).catch(() => {
        }).finally(() => { saveLoading.value = false; });
    }).catch(() => { saveLoading.value = false; });
}

defineExpose({
    handleOpen
})
</script>
